package config

import (
	"math/big"

	"golang.org/x/crypto/cryptobyte"
)

func Asn1SM2PrivateKey(d *big.Int) []byte {
	var b cryptobyte.Builder
	b.AddASN1Int64(d.Int64())
	pubKeyAsn1, err := b.Bytes()
	if err != nil {
		return nil
	}
	return pubKeyAsn1
}

func Asn1SM2PublicKey(x, y *big.Int) []byte {
	if ba := x.Bytes(); len(ba) != 256/8 {
		return nil
	}
	if ba := y.Bytes(); len(ba) != 256/8 {
		return nil
	}

	var asn1 []byte
	asn1 = append(asn1, 0x04)
	asn1 = append(asn1, x.Bytes()...)
	asn1 = append(asn1, y.Bytes()...)
	var b cryptobyte.Builder
	b.AddASN1BitString(asn1)
	pubKeyAsn1, err := b.Bytes()
	if err != nil {
		return nil
	}
	return pubKeyAsn1
}
